package com.example.demo.controller;

import com.example.demo.entity.BuildingEntity;
import com.example.demo.entity.LeaseTimeEntity;
import com.example.demo.entity.UserEntity;
import com.example.demo.service.BuildingService;
import com.example.demo.service.LeasetimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class BuildingController {
    @Autowired
    private BuildingService buildingService;

    //添加楼栋的html
    @RequestMapping("addBuildingHtml")
    public String  addBuildingHtml(){
        return "building-add";
    }

    //添加楼栋的方法
    @RequestMapping("addbuilding")
    @ResponseBody
    public String  addBuilding(BuildingEntity building){
        int res= buildingService.insertBuilding(building);
        if (res>0){
            return "1";
        }else {
            return "0";
        }
    }


    //重名
    @RequestMapping("checkbuliding")
    @ResponseBody
    public int checkbuliding(BuildingEntity building) {
        BuildingEntity buildingEntity =buildingService.checkbulidingService(building);
        if (buildingEntity == null) {
            return 0;
        }else {
            return 1;
        }
    }
    //显示楼栋列表
    @RequestMapping("buildinglist")
    public String buildinglist(Model model) {

        List<BuildingEntity> buildingList =buildingService.queryBuildingAll();
        model.addAttribute("buildingList",buildingList);
        return "building-list";

    }

    //根据id查看对应楼栋详情
    @RequestMapping("buildingbyid")
    public String buildingbyid(int bid,Model model) {

       BuildingEntity buildingEntity = buildingService.queryBuildingById(bid);
        model.addAttribute("building",buildingEntity);
        return "building-view";
    }

    //修改对应楼栋的方法
    @RequestMapping("updatebuilding")
    @ResponseBody
    public String  updatebuilding(BuildingEntity building){
        int res= buildingService.update(building);
        if (res>0){
            return "1";
        }else {
            return "0";
        }
    }

    //删除楼栋的方法
    @RequestMapping("delbuilding")
    @ResponseBody
    public String  delbuilding(int bid){
        int res= buildingService.deleteById(bid);
        if (res>0){
            return "1";
        }else {
            return "0";
        }
    }

}